package cn.succy.spider.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ConnectException;
import java.net.URL;
import java.util.List;

import javax.net.ssl.HttpsURLConnection;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import cn.succy.spider.bean.Girl;

public class UrlUtil {

	public static String getUrlContent(String urlStr, String encoding) throws IOException {

		URL url = new URL(urlStr);
		HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
		StringBuffer buf = new StringBuffer();
		BufferedReader br =null;
		try {
			br= new BufferedReader(
					new InputStreamReader(conn.getInputStream(), encoding));
		} catch (ConnectException e) {
			//e.printStackTrace();
			return null;
		}
		String line = null;
		while ((line = br.readLine()) != null) {
			// System.out.println(line);
			buf.append(line + "\r\n");
		}
		
		if(br != null)
			br.close();
		return buf.toString();
	}

	public static  List<Girl> JSONParser(String jsonString) {
		JSONArray jsonArray = JSON.parseObject(jsonString).getJSONObject("data")
				.getJSONArray("searchDOList");
		return JSONArray.parseArray(jsonArray.toJSONString(), Girl.class);
	}
}
